home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elib-006.lha / elib-0.06 / library / dll.el < prev    next >
Lisp/Scheme  |  1993-01-24  |  11KB  |  387 lines

  1. ;;; $Id: dll.el,v 1.6 1993/01/14 16:27:28 ceder Exp $
  2. ;;; elib-dll.el -- Some primitives for Doubly linked lists.
  3. ;;; Copyright (C) 1991, 1992  Per Cederqvist
  4. ;;;
  5. ;;; This program is free software; you can redistribute it and/or modify
  6. ;;; it under the terms of the GNU General Public License as published by
  7. ;;; the Free Software Foundation; either version 2 of the License, or
  8. ;;; (at your option) any later version.
  9. ;;;
  10. ;;; This program is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;;; GNU General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU General Public License
  16. ;;; along with this program; if not, write to the Free Software
  17. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. ;;; Mail bug reports to ceder@lysator.liu.se.
  20.  
  21. (require 'elib-node)
  22. (provide 'dll)
  23.  
  24. ;;;
  25. ;;; A doubly linked list consists of one cons cell which holds the tag
  26. ;;; 'DL-LIST in the car cell and a pointer to a dummy node in the cdr
  27. ;;; cell. The doubly linked list is implemented as a circular list
  28. ;;; with the dummy node first and last. The dummy node is recognized
  29. ;;; by comparing it to the node which the cdr of the cons cell points
  30. ;;; to.
  31. ;;;
  32.  
  33. ;;; ================================================================
  34. ;;;      Internal functions for use in the doubly linked list package
  35.  
  36. (defun dll-get-dummy-node (dll)
  37.  
  38.   ;; Return the dummy node.   INTERNAL USE ONLY.
  39.   (cdr dll))
  40.  
  41. (defun dll-list-nodes (dll)
  42.  
  43.   ;; Return a list of all nodes in DLL.   INTERNAL USE ONLY.
  44.  
  45.   (let* ((result nil)
  46.      (dummy  (dll-get-dummy-node dll))
  47.      (node   (elib-node-left dummy)))
  48.  
  49.     (while (not (eq node dummy))
  50.       (setq result (cons node result))
  51.       (setq node (elib-node-left node)))
  52.  
  53.     result))
  54.  
  55. (defun dll-set-from-node-list (dll list)
  56.  
  57.   ;; Set the contents of DLL to the nodes in LIST.
  58.   ;; INTERNAL USE ONLY.
  59.  
  60.   (dll-clear dll)
  61.   (let* ((dummy (dll-get-dummy-node dll))
  62.      (left  dummy))
  63.     (while list
  64.       (elib-node-set-left (car list) left)
  65.       (elib-node-set-right left (car list))
  66.       (setq left (car list))
  67.       (setq list (cdr list)))
  68.  
  69.     (elib-node-set-right left dummy)
  70.     (elib-node-set-left dummy left)))
  71.  
  72.  
  73. ;;; ===================================================================
  74. ;;;       The public functions which operate on doubly linked lists.
  75.  
  76. (defmacro dll-element (dll node)
  77.  
  78.   "Get the element of a NODE in a doubly linked list DLL.
  79. Args: DLL NODE."
  80.  
  81.   (` (elib-node-data (, node))))
  82.  
  83.  
  84. (defun dll-create ()
  85.   "Create an empty doubly linked list."
  86.   (let ((dummy-node (elib-node-create nil nil nil)))
  87.     (elib-node-set-right dummy-node dummy-node)
  88.     (elib-node-set-left dummy-node dummy-node)
  89.     (cons 'DL-LIST dummy-node)))
  90.  
  91. (defun dll-p (object)
  92.   "Return t if OBJECT is a doubly linked list, otherwise return nil."
  93.   (eq (car-safe object) 'DL-LIST))
  94.  
  95. (defun dll-enter-first (dll element)
  96.   "Add an element first on a doubly linked list.
  97. Args: DLL ELEMENT."
  98.   (dll-enter-after
  99.    dll
  100.    (dll-get-dummy-node dll)
  101.    element))
  102.  
  103.  
  104. (defun dll-enter-last (dll element)
  105.   "Add an element last on a doubly linked list.
  106. Args: DLL ELEMENT."
  107.   (dll-enter-before
  108.    dll
  109.    (dll-get-dummy-node dll)
  110.    element))
  111.  
  112.  
  113. (defun dll-enter-after (dll node element)
  114.   "In the doubly linked list DLL, insert a node containing ELEMENT after NODE.
  115. Args: DLL NODE ELEMENT."
  116.  
  117.   (let ((new-node (elib-node-create
  118.            node (elib-node-right node)
  119.            element)))
  120.     (elib-node-set-left (elib-node-right node) new-node)
  121.     (elib-node-set-right node new-node)))
  122.  
  123.  
  124. (defun dll-enter-before (dll node element)
  125.   "In the doubly linked list DLL, insert a node containing ELEMENT before NODE.
  126. Args: DLL NODE ELEMENT."
  127.   
  128.   (let ((new-node (elib-node-create
  129.            (elib-node-left node) node
  130.            element)))
  131.     (elib-node-set-right (elib-node-left node) new-node)
  132.     (elib-node-set-left node new-node)))
  133.  
  134.  
  135.  
  136. (defun dll-next (dll node)
  137.   "Return the node after NODE, or nil if NODE is the last node.
  138. Args: DLL NODE."
  139.  
  140.   (if (eq (elib-node-right node) (dll-get-dummy-node dll))
  141.       nil
  142.     (elib-node-right node)))
  143.  
  144.  
  145. (defun dll-previous (dll node)
  146.   "Return the node before NODE, or nil if NODE is the first node.
  147. Args: DLL NODE."
  148.  
  149.   (if (eq (elib-node-left node) (dll-get-dummy-node dll))
  150.       nil
  151.     (elib-node-left node)))
  152.  
  153.  
  154. (defun dll-delete (dll node)
  155.  
  156.   "Delete NODE from the doubly linked list DLL.
  157. Args: DLL NODE. Return the element of node."
  158.  
  159.   ;; This is a no-op when applied to the dummy node. This will return
  160.   ;; nil if applied to the dummy node since it always contains nil.
  161.  
  162.   (elib-node-set-right (elib-node-left node) (elib-node-right node))
  163.   (elib-node-set-left (elib-node-right node) (elib-node-left node))
  164.   (dll-element dll node))
  165.  
  166.  
  167.  
  168. (defun dll-delete-first (dll)
  169.  
  170.   "Delete the first NODE from the doubly linked list DLL.
  171. Return the element. Args: DLL. Returns nil if the DLL was empty."
  172.  
  173.   ;; Relies on the fact that dll-delete does nothing and
  174.   ;; returns nil if given the dummy node.
  175.  
  176.   (dll-delete dll (elib-node-right (dll-get-dummy-node dll))))
  177.  
  178.  
  179. (defun dll-delete-last (dll)
  180.  
  181.   "Delete the last NODE from the doubly linked list DLL.
  182. Return the element. Args: DLL. Returns nil if the DLL was empty."
  183.  
  184.   ;; Relies on the fact that dll-delete does nothing and
  185.   ;; returns nil if given the dummy node.
  186.  
  187.   (dll-delete dll (elib-node-left (dll-get-dummy-node dll))))
  188.  
  189.  
  190. (defun dll-first (dll)
  191.  
  192.   "Return the first element on the doubly linked list DLL.
  193. Return nil if the list is empty. The element is not removed."
  194.  
  195.   (if (eq (elib-node-right (dll-get-dummy-node dll))
  196.       (dll-get-dummy-node dll))
  197.       nil
  198.     (elib-node-data (elib-node-right (dll-get-dummy-node dll)))))
  199.  
  200.  
  201.  
  202.  
  203. (defun dll-last (dll)
  204.  
  205.   "Return the last element on the doubly linked list DLL.
  206. Return nil if the list is empty. The element is not removed."
  207.  
  208.   (if (eq (elib-node-left (dll-get-dummy-node dll))
  209.       (dll-get-dummy-node dll))
  210.       nil
  211.     (elib-node-data (elib-node-left (dll-get-dummy-node dll)))))
  212.  
  213.  
  214.  
  215. (defun dll-nth (dll n)
  216.  
  217.   "Return the Nth node from the doubly linked list DLL.
  218.  Args: DLL N
  219. N counts from zero. If DLL is not that long, nil is returned.
  220. If N is negative, return the -(N+1)th last element.
  221. Thus, (dll-nth dll 0) returns the first node,
  222. and (dll-nth dll -1) returns the last node."
  223.  
  224.   ;; Branch 0 ("follow left pointer") is used when n is negative.
  225.   ;; Branch 1 ("follow right pointer") is used otherwise.
  226.  
  227.   (let* ((dummy  (dll-get-dummy-node dll))
  228.      (branch (if (< n 0) 0 1))
  229.      (node   (elib-node-branch dummy branch)))
  230.      
  231.     (if (< n 0)
  232.     (setq n (- -1 n)))
  233.  
  234.     (while (and (not (eq dummy node))
  235.         (> n 0))
  236.       (setq node (elib-node-branch node branch))
  237.       (setq n (1- n)))
  238.  
  239.     (if (eq dummy node)
  240.     nil
  241.       node)))
  242.  
  243.  
  244. (defun dll-empty (dll)
  245.  
  246.   "Return t if the doubly linked list DLL is empty, nil otherwise"
  247.  
  248.   (eq (elib-node-left (dll-get-dummy-node dll))
  249.       (dll-get-dummy-node dll)))
  250.  
  251. (defun dll-length (dll)
  252.  
  253.   "Returns the number of elements in the doubly linked list DLL."
  254.  
  255.   (let*  ((dummy (dll-get-dummy-node dll))
  256.       (node  (elib-node-right dummy))
  257.       (n     0))
  258.  
  259.     (while (not (eq node dummy))
  260.       (setq node (elib-node-right node))
  261.       (setq n (1+ n)))
  262.  
  263.     n))
  264.  
  265.  
  266.  
  267. (defun dll-copy (dll &optional element-copy-fnc)
  268.  
  269.   "Return a copy of the doubly linked list DLL.
  270. If optional second argument ELEMENT-COPY-FNC is non-nil it should be
  271. a function that takes one argument, an element, and returns a copy of it.
  272. If ELEMENT-COPY-FNC is not given the elements are not copied."
  273.  
  274.   (let ((result (dll-create))
  275.     (node (dll-nth dll 0)))
  276.     (if element-copy-fnc
  277.  
  278.     ;; Copy the elements with the user-supplied function.
  279.     (while node
  280.       (dll-enter-last result
  281.               (funcall element-copy-fnc
  282.                    (dll-element dll node)))
  283.       (setq node (dll-next dll node)))
  284.  
  285.       ;; Don't try to copy the elements - they might be
  286.       ;; circular lists, or anything at all...
  287.       (while node
  288.     (dll-enter-last result (dll-element dll node))
  289.     (setq node (dll-next dll node))))
  290.     
  291.     result))
  292.  
  293.  
  294.  
  295. (defun dll-all (dll)
  296.  
  297.   "Return all elements on the double linked list DLL as an ordinary list."
  298.  
  299.   (let* ((result nil)
  300.      (dummy  (dll-get-dummy-node dll))
  301.      (node   (elib-node-left dummy)))
  302.  
  303.     (while (not (eq node dummy))
  304.       (setq result (cons (dll-element dll node) result))
  305.       (setq node (elib-node-left node)))
  306.  
  307.     result))
  308.  
  309.  
  310. (defun dll-clear (dll)
  311.  
  312.   "Clear the doubly linked list DLL, i.e. make it completely empty."
  313.  
  314.   (elib-node-set-left (dll-get-dummy-node dll) (dll-get-dummy-node dll))
  315.   (elib-node-set-right (dll-get-dummy-node dll) (dll-get-dummy-node dll)))
  316.  
  317.  
  318. (defun dll-map (map-function dll)
  319.  
  320.   "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
  321. The function is applied to the first element first."
  322.  
  323.   (let*  ((dummy (dll-get-dummy-node dll))
  324.       (node  (elib-node-right dummy)))
  325.  
  326.     (while (not (eq node dummy))
  327.       (funcall map-function (dll-element dll node))
  328.       (setq node (elib-node-right node)))))
  329.  
  330.  
  331. (defun dll-map-reverse (map-function dll)
  332.  
  333.   "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
  334. The function is applied to the last element first."
  335.  
  336.   (let*  ((dummy (dll-get-dummy-node dll))
  337.       (node  (elib-node-left dummy)))
  338.  
  339.     (while (not (eq node dummy))
  340.       (funcall map-function (dll-element dll node))
  341.       (setq node (elib-node-left node)))))
  342.  
  343.  
  344. (defun dll-create-from-list (list)
  345.  
  346.   "Given an elisp LIST create a doubly linked list with the same elements."
  347.  
  348.   (let ((dll (dll-create)))
  349.     (while list
  350.       (dll-enter-last dll (car list))
  351.       (setq list (cdr list)))
  352.     dll))
  353.  
  354.  
  355.  
  356. (defun dll-sort (dll predicate)
  357.  
  358.   "Sort the doubly linked list DLL, stably, comparing elements using PREDICATE.
  359. Returns the sorted list. DLL is modified by side effects.
  360. PREDICATE is called with two elements of DLL, and should return T
  361. if the first element is \"less\" than the second."
  362.  
  363.   (dll-set-from-node-list
  364.    dll (sort (dll-list-nodes dll)
  365.          (function (lambda (x1 x2)
  366.              (funcall predicate
  367.                   (dll-element dll x1)
  368.                   (dll-element dll x2))))))
  369.   dll)
  370.  
  371.  
  372. (defun dll-filter (dll predicate)
  373.  
  374.   "Remove all elements in the doubly linked list DLL for which PREDICATE
  375. returns nil."
  376.  
  377.   (let* ((dummy (dll-get-dummy-node dll))
  378.      (node  (elib-node-right dummy))
  379.      next)
  380.  
  381.     (while (not (eq node dummy))
  382.       (setq next (elib-node-right node))
  383.       (if (funcall predicate (dll-element dll node))
  384.       nil
  385.     (dll-delete dll node))
  386.       (setq node next))))
  387.